package com.cn.yz.springboot.util.tablesplit.impl;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.cn.yz.springboot.exception.TableSplitException;
import com.cn.yz.springboot.util.ObjTransUtils;
import com.cn.yz.springboot.util.TableSplitUtil;
import com.cn.yz.springboot.util.tablesplit.Strategy;

/**
 * 只支持Map和Object对象
 * @author king
 *
 */
public class RemainderStrategy implements Strategy {

	private Logger logger = LoggerFactory.getLogger(RemainderStrategy.class);
	
	@Override
	public String convert(Map<String, Object> params) throws Exception {
		StringBuilder sb=new StringBuilder(params.get(Strategy.TABLE_NAME).toString());
		sb.append("_");
		String field=params.get(Strategy.SPLIT_FIELD).toString();
		String fieldKey = TableSplitUtil.fieldKeyTransField(field);
		Object paramValue= params.get(Strategy.EXECUTE_PARAM_VALUES);
		String value = "";
		Map<String, Object> paramMap =TableSplitUtil.paramMapTransObj(paramValue);
		if(paramMap.get(fieldKey.toString())!=null){
			value = paramMap.get(fieldKey.toString()).toString();
		}else{
			throw new TableSplitException(params.get(Strategy.TABLE_NAME).toString(),field);
		}
		sb.append(value);
		return sb.toString();
	}

}
